<!-- 借阅车  -->
<template>
    <div style="padding: var(--main-padding)">
        <div class="Borrow">
            <div class="background">
                <el-popconfirm
                    title="确定清空借阅车吗？"
                    confirm-button-text="删除"
                    cancel-button-text="取消"
                    icon="el-icon-info"
                    icon-color="red"
                    @confirm="Empty"
                >
                    <el-button type="warning" slot="reference" plain>清空借阅车</el-button>
                </el-popconfirm>

                <el-popconfirm
                    title="确定删除选中项吗？"
                    confirm-button-text="删除"
                    cancel-button-text="取消"
                    icon="el-icon-info"
                    icon-color="red"
                    @confirm="deleteFile"
                >
                    <el-button type="danger" slot="reference" plain>删除</el-button>
                </el-popconfirm>
            </div>
            <zCollapse :zCollapseData="BorrowData" ref="zCollapse" />

            <div class="Borrow_footer">
                <el-button type="primary" @click="$router.push('/AttributeQuery')">返回</el-button>
                <el-button type="primary" @click="onSubmit">申请</el-button>
            </div>
        </div>
        <zdialog ref="dialog" :dataInfo="dataInfo" />
    </div>
</template>

<script>
import zCollapse from "@/components/zCollapse"

import zdialog from "./dialog.vue"

export default {
    name: "",
    data() {
        return {
            BorrowData: [],
            dataInfo: [],
        }
    },

    components: { zCollapse, zdialog },

    computed: {},

    mounted() {
        this.getBorrowData()
        this.$store.state.map.mapList = []
    },

    methods: {
        async onSubmit() {
            const data = await this.$store.dispatch("user/getInfo")

            if (data.code == 200) {
                if (data.data.idNumber == null || !data.data.idNumber) return this.getUserInfo()
                if (data.data.workCompany == null || !data.data.workCompany) return this.getUserInfo()
                if (data.data.unitProperties == null || !data.data.unitProperties) return this.getUserInfo()
                if (data.data.profession == null || !data.data.profession) return this.getUserInfo()
                if (data.data.companyAddress == null || !data.data.companyAddress) return this.getUserInfo()

                if (!this.$refs.zCollapse.setCheckData()) return
                this.dataInfo = this.$refs.zCollapse.getSelectData()

                const isFileNo = await this.isFileNo()
                if (isFileNo == 0) return

                this.$refs.dialog.handleClose()
            } else {
                this.$message.error(data.msg || "获取个人信息失败，请重试")
            }
        },
        async getBorrowData() {
            const { data } = await this.$store.dispatch("borrowCar/getBorrowingCar")

            let preindex = 0
            this.BorrowData = data.reduce((pre, item, index) => {
                let data = {
                    name: item.title,
                    snId: item.snId,
                    uploadTime: item.uploadTime,
                    indeterminate: false,
                    selected: false,
                    userId: item.userId,
                    data: [],
                    id: item.id,
                }
                let fileData = {
                    id: item.id,
                    fileId: item.fileId,
                    name: item.fileName,
                    selected: false,
                    fileNo: item.fileNo,
                    fileCategory: item.fileCategory,
                }
                if (pre.length == 0) {
                    pre.push(data)
                    pre[preindex].data.push(fileData)
                    return pre
                }
                if (pre.some((e) => e.snId == item.snId)) {
                    pre[preindex].data.push(fileData)
                } else {
                    pre.push(data)
                    preindex++
                    pre[preindex].data.push(fileData)
                }
                return pre
            }, [])
        },
        async Empty() {
            const { msg, code } = await this.$store.dispatch("borrowCar/deleteBorrowingCarAll")
            if (code == 200) this.$message.success(msg || "删除成功")
            this.getBorrowData()
        },
        async deleteFile() {
            if (!this.$refs.zCollapse.setCheckData()) return

            const { msg, code } = await this.$store.dispatch("borrowCar/deleteBorrowingCarFile", this.$refs.zCollapse.setCheckData())
            if (code == 200) this.$message.success(msg || "删除成功")
            this.getBorrowData()
        },
        //判断用户信息是否完善
        getUserInfo(data) {
            this.$confirm("请完善信息", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warnimg",
            })
                .then((status) => {
                    if (status == "confirm") {
                        this.$router.push("/UserInfo")
                    }
                })
                .catch((status) => {
                    if (status == "cancel") {
                    }
                })
        },
        async isFileNo() {
            let arr = this.dataInfo.map((e) => {
                return e.snId + ""
            })
            const { code } = await this.$store.dispatch("borrowCar/GetIsValid", arr)

            return code
        },
    },
}
</script>
<style lang='scss' scoped>
.Borrow {
    background: white;
    padding: 40px;
    min-height: $height_full;

    .Borrow_footer {
        padding: 20px 0;
        text-align: center;
        // position: fixed;
        // bottom: 110px;
        // left: 50%;
        margin-bottom: 100px;
    }

    .background {
        background: white;
        padding: 10px;
    }

    .el-button {
        margin-right: 20px;
    }
}
</style>